package com.tensquare.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/31-14:22
 * ------------------------------
 */
@Configuration
public class MyGatewayConfig {

//     @Bean
//     public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
//         return builder.routes()
//                 .route("tensquare-gathering",
//                             r->r.path("/gathering/**")
//                                 .filters(f->f.stripPrefix(1))
//                                 .uri("lb://tensquare-gathering")
//                 ).build();
//     }

    public MyGatewayConfig(){
        GatewayCallbackManager.setBlockHandler((serverWebExchange, e) -> {
            Map map = new HashMap<>();
            map.put("msg","限流异常！");
            Mono<ServerResponse> responseMono = ServerResponse.ok().body(Mono.just(JSON.toJSONString(map)), String.class);
            return responseMono;
        });
    }
 }
